package com.mingle.iwalk;

import java.io.IOException;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class VoiceDialog extends Dialog {
	
	private MediaPlayer mPlayer;
	private POIInfo mInfo;
	private TextView mTimeInfo;
	private CheckBox mPlayBtn;
	private SeekBar mPlaySeekBar;
	private View.OnClickListener mDeleteListener;
	
	private final int HANDLER_MSG_UPDATE_TIME = 0;
	private Handler mHandler= new Handler()
	{
		@Override
        public void handleMessage(Message msg) {

            switch(msg.what) {
            case HANDLER_MSG_UPDATE_TIME:
            {
            	if(updateTimeInfo())
            	{
            		mPlayBtn.setChecked(false);
            	}
            	else
            	{
            		Message m = mHandler.obtainMessage(HANDLER_MSG_UPDATE_TIME, 0, 0, null);
            		mHandler.sendMessageDelayed(m,1000);
            	}
            	updateSeekBar();
            	
            }
            break;
            }
		}
	};
	
	private void sendMessage(int type)
	{
		Message m = mHandler.obtainMessage(type, 0, 0, null);
		mHandler.sendMessageDelayed(m, 1000);
	}
	
	
	public VoiceDialog(Context context, String path, POIInfo info, View.OnClickListener deleteListener)
	{
		super(context, R.style.dialog);
		
		mDeleteListener = deleteListener;
		if(info == null || info.id == TravelerInfo.INFO_INVALID_ID)
		{
			if(path == null || path.length() == 0)
			{
				this.dismiss();
				return;
			}
			
			mInfo = new POIInfo();
			mInfo.viocePath = path;
		}
		else
		{
			mInfo = info;
		}
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.voice_dlg_layout); 

		Button exitBtn = (Button) findViewById(R.id.exit_btn);
		exitBtn.setOnClickListener(mBtnClickListener);
		
		Button deleteBtn = (Button) findViewById(R.id.delete_btn);
		deleteBtn.setOnClickListener(mBtnClickListener);
		deleteBtn.setVisibility(mInfo.id==TravelerInfo.INFO_INVALID_ID?View.VISIBLE:View.INVISIBLE);;

		
		mPlayBtn = (CheckBox) findViewById(R.id.play_btn);
		mPlayBtn.setOnCheckedChangeListener(mPlayChangedListener);
		
		mPlaySeekBar = (SeekBar) findViewById(R.id.voice_dlg_time_bar);
		mPlaySeekBar.setOnSeekBarChangeListener(mPlayBarSeekListener);

		mTimeInfo = (TextView) findViewById(R.id.voice_dlg_time);
		
	}
	
	private boolean updateTimeInfo()
	{
		if(mPlayer == null)
		{
			return true;
		}
		int curTime = mPlayer.getCurrentPosition();
    	int totalTime = mPlayer.getDuration();
    	String time = curTime/1000 +" / " + totalTime/1000;
    	
    	if(Math.abs(totalTime - curTime)<200)
    	{
    		time = 0 +" / " + totalTime/1000;
    	}
    	mTimeInfo.setText(time);
    	
		return Math.abs(curTime - totalTime)<200;
	}
	
	private void updateSeekBar()
	{
		if(mPlayer == null)
		{
			return;
		}
		
		int cur = mPlayer.getCurrentPosition()/1000;
		int total = mPlayer.getDuration()/1000;
		mPlaySeekBar.setMax(total);
		mPlaySeekBar.setProgress(cur!= total?cur:0);
	}
	
	private OnCheckedChangeListener mPlayChangedListener = new OnCheckedChangeListener()
	{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if(mPlayer == null)
			{
				return;
			}
			
			//play
			if(isChecked)
			{
				mPlayer.start();
				sendMessage(HANDLER_MSG_UPDATE_TIME);
			}
			//stop
			else
			{
				if(mPlayer.isPlaying())
				{
					mPlayer.pause();
				}
				else
				{
					mPlayer.stop();
					mPlayer.reset();
					
					try {
						mPlayer.setDataSource(mInfo.viocePath);
						mPlayer.prepare();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				mHandler.removeMessages(HANDLER_MSG_UPDATE_TIME);
			}
			
		}
		
	};
	
	private OnSeekBarChangeListener mPlayBarSeekListener = new OnSeekBarChangeListener()
	{

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			
			mPlayer.seekTo(progress*1000);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
			
		}
		
	};

	private View.OnClickListener mBtnClickListener = new View.OnClickListener()
	{

		@Override
		public void onClick(View v) {
			
			switch(v.getId())
			{
			case R.id.exit_btn:
			{
				dismiss();
			}
			break;
			
			case R.id.delete_btn:
			{
				//TODO:
				if(mDeleteListener!=null)
				{
					mDeleteListener.onClick(v);
					mDeleteListener = null;
				}
				dismiss();
			}
			break;
			}
			
		}

	};

	@Override
	protected void onStart() {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(mInfo.viocePath);
			mPlayer.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		updateTimeInfo();
		updateSeekBar();
		
		super.onStart();
	}

	@Override
	protected void onStop() {
		if(mPlayer != null)
		{
			if(mPlayer.isPlaying())
			{
				mPlayer.stop();
			}
			
			mPlayer.release();
			mPlayer = null;
		}
		super.onStop();
	}
	
	
}
